package com.atguigu.szdz.dcsyl.service.impl;

import com.atguigu.szdz.dcsyl.dao.WellInfoDao;
import com.atguigu.szdz.dcsyl.service.WellInfoService;
import com.atguigu.szdz.dcsyl.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("wellInfoService")
public class WellInfoServiceImpl implements WellInfoService {

    @Autowired
    private MapperUtils mapperUtils;
    @Autowired
    private WellInfoDao wellInfoDao;

    @Override
    public List<Map<String, Object>> getWellInfoList(Integer currentPage, String keyword) {
        String sql = "";
        //如果currentPage为null，就查所有
        if (currentPage == null || currentPage == 0) {
            sql = "select distinct on (wi.id) wi.*,pw.project_id from dc_well_info wi left join dc_project_well pw on wi.id = pw.well_id order by wi.id asc";
        }else {
            sql = "select distinct on (wi.id) wi.*,pw.project_id from dc_well_info wi left join dc_project_well pw on wi.id = pw.well_id where wi.well_name like '%"+keyword+"%' order by wi.id asc limit 10 offset " + (currentPage - 1) * 10;
        }
        return mapperUtils.queryBySql(sql);
    }

    @Override
    public void addWellInfo(Map<String, Object> map) {
        Integer project_id = (Integer) map.get("project_id");
        String well_name = (String) map.get("well_name");
        Double locx = map.get("locx") != null && !map.get("locx").toString().isEmpty() ? Double.valueOf(map.get("locx").toString()) : 0.0;
        Double locy = map.get("locy") != null && !map.get("locy").toString().isEmpty() ? Double.valueOf(map.get("locy").toString()) : 0.0;
        Double kb = map.get("kb") != null && !map.get("kb").toString().isEmpty() ? Double.valueOf(map.get("kb").toString()) : 0.0;
        Double wd = map.get("wd") != null && !map.get("wd").toString().isEmpty() ? Double.valueOf(map.get("wd").toString()) : 0.0;
        String workunit = map.get("workunit") != null && !map.get("workunit").toString().isEmpty() ? (String) map.get("workunit") : "";
        String operator = map.get("operator") != null && !map.get("operator").toString().isEmpty() ? (String) map.get("operator") : "";
        Map<String,Object> map1 = new HashMap<>();
        map1.put("well_name",well_name);
        map1.put("create_date",LocalDate.now());
        map1.put("update_date",LocalDate.now());
        map1.put("locx",locx);
        map1.put("locy",locy);
        map1.put("kb",kb);
        map1.put("wd",wd);
        map1.put("workunit",workunit);
        map1.put("operator",operator);
        wellInfoDao.insertWellInfo(map1);
        //添加项目与井的关联关系
        String sql1 = "insert into dc_project_well (project_id, well_id) values ("+project_id+","+map1.get("id")+")";
        mapperUtils.updataBySql(sql1);
    }

    @Override
    public void updateWellInfo(Map<String, Object> map) {
        String well_name = (String) map.get("well_name");
        Double locx = map.get("locx") != null && !map.get("locx").toString().isEmpty() ? Double.valueOf(map.get("locx").toString()) : 0.0;
        Double locy = map.get("locy") != null && !map.get("locy").toString().isEmpty() ? Double.valueOf(map.get("locy").toString()) : 0.0;
        Double kb = map.get("kb") != null && !map.get("kb").toString().isEmpty() ? Double.valueOf(map.get("kb").toString()) : 0.0;
        Double wd = map.get("wd") != null && !map.get("wd").toString().isEmpty() ? Double.valueOf(map.get("wd").toString()) : 0.0;
        String update_date = String.valueOf(LocalDate.now());
        String workunit = map.get("workunit") != null && !map.get("workunit").toString().isEmpty() ? (String) map.get("workunit") : "";
        String operator = map.get("operator") != null && !map.get("operator").toString().isEmpty() ? (String) map.get("operator") : "";
        String sql = "update dc_well_info set well_name = '"+well_name+"', locx = "+locx+", locy = "+locy+", kb = "+kb+", wd = "+wd+", workunit = '"+workunit+"', operator = '"+operator+"', update_date = '"+update_date+"' where id = "+map.get("id");
        mapperUtils.updataBySql(sql);
    }

    @Override
    public void deleteWellInfo(Integer well_id) {
        //删除井信息前，先删除关联的井数据
        String sql1 = "delete from dc_project_well where well_id = "+well_id;
        mapperUtils.updataBySql(sql1);
        //删除井信息
        String sql2 = "delete from dc_well_info where id = "+well_id;
        mapperUtils.updataBySql(sql2);
    }

    @Override
    public Map<String, Object> getWellInfoDetail(Integer well_id) {
        String sql = "select * from dc_well_info where id = "+well_id;
        List<Map<String, Object>> list = mapperUtils.queryBySql(sql);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return Collections.emptyMap();
    }

    @Override
    public List<Map<String, Object>> getDepthList(Integer well_id) {
        String sql = "select id,depth,nhg from \"dc_ylDataDc\" where well_id = "+well_id;
        return mapperUtils.queryBySql(sql);
    }
}
